package ccgl.com.action;

import java.io.IOException;
import java.sql.SQLException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import ccgl.com.model.MKh;
import ccgl.com.model.MKhSearchItem;
import ccgl.com.model.MUser;
import ccgl.com.server.KhglServer;
import ccgl.com.util.GsonPrint;
import ccgl.com.util.JsonHelper;

/**
 * 订单Action
 * 
 * @author Administrator
 * 
 */
public class KhglAction extends BaseServlet {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	HttpServletRequest request = null;
	HttpServletResponse response = null;
	KhglServer server = new KhglServer();

	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		doPost(req, resp);
	}

	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		this.request = req;
		this.response = resp;
		req.setCharacterEncoding("UTF-8");
		String control = this.request.getParameter("control");
		if ("saveKhxx".equals(control)) {
			addOrUpdataKhxx();
		} else if ("getKhList".equals(control)) {
			getKhList();
		} else if ("doView".equals(control)) {
			doView();
		} else if ("doEdit".equals(control)) {
			doEdit();
		} else if ("doDelete".equals(control)) {
			doDelete();
		} else if ("getKhSearchList".equals(control)) {
			getDdSearchList();
		} else {
			GsonPrint.printFormatGson("没有请求的control！", this.response);
		}
	}

	private void getDdSearchList() {
		MKhSearchItem item = new MKhSearchItem();
		item.setPage(parseInt(this.request.getParameter("page")));
		item.setPagesize(parseInt(this.request.getParameter("pagesize")));
		item.setXm(this.request.getParameter("khxm"));
		try {
			int star = (item.getPage() - 1) * item.getPagesize();// 从0开始
			int end = item.getPage() * item.getPagesize();
			MUser user = (MUser) this.request.getSession().getAttribute("user");
			if (user != null) {
				GsonPrint.printGson(
						server.getKhListWithParams(item, star, end,
								user.getId()), this.response);
			} else {
				this.request.setAttribute("error", "请登录！");
				this.request.getRequestDispatcher("/login.jsp").forward(
						request, response);
			}
		} catch (IOException e) {
			e.printStackTrace();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (ServletException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	private void doDelete() {
		try {
			if (server.deleteKhxxs(this.request.getParameter("id"))) {
				GsonPrint.printFormatGson("删除成功", this.response);
			} else {
				GsonPrint.printFormatGson("删除出错", this.response);
			}
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}

	// 得到商品list
	private void getKhList() {
		int page = parseInt(this.request.getParameter("page"));// 当前页数
		int pagesize = parseInt(this.request.getParameter("pagesize"));// 当前页数的大小
		MUser user = (MUser) this.request.getSession().getAttribute("user");
		int star = (page - 1) * pagesize;// 从0开始
		int end = page * pagesize;
		try {
			if (user != null) {
				GsonPrint.printGson(server.getKhList(star, end, user.getId()),
						response);
			} else {
				this.request.setAttribute("error", "请登录！");
				this.request.getRequestDispatcher("/login.jsp").forward(
						request, response);
			}
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (ServletException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}

	/**
	 * 增加
	 */
	private void addOrUpdataKhxx() {
		MKh kh = null;
		try {
			kh = JsonHelper.getObjectByServletRequest(this.request, MKh.class);
			if (kh == null || kh.toString() == null) {
				GsonPrint.printFormatGson("提交信息出错！", this.response);
				return;
			}
			String res = null;
			if (kh.getId() == null || kh.getId().trim().length() == 0) {
				res = server.addKhxx(kh);
			} else {
				res = server.updateKhxx(kh);
			}
			GsonPrint.printFormatGson(res, this.response);
		} catch (Exception e) {
			try {
				GsonPrint.printFormatGson("提交信息转换出错！", this.response);
			} catch (IOException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			}
			e.printStackTrace();
		}
	}

	// 编辑数据传递到编辑页面
	private void doEdit() {
		this.request.setAttribute("rowJsonStringData",
				this.request.getParameter("jsonobj"));
		try {
			this.request.getRequestDispatcher("/jsp/khgl_xz.jsp").forward(
					this.request, this.response);
		} catch (ServletException | IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	// 查看数据传递到页面
	private void doView() {
		this.request.setAttribute("rowJsonStringData",
				this.request.getParameter("jsonobj"));
		this.request.setAttribute("isReadOnly", "true");
		try {
			this.request.getRequestDispatcher("/jsp/khgl_xz.jsp").forward(
					this.request, this.response);
		} catch (ServletException | IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
}
